﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        TextBox textBox1 = new TextBox();
        Panel panelNumber = new Panel();
        Panel panelActoin = new Panel();  
        private int a;
        private int b;
        private char operation;

        public Form1()
        {
            InitializeComponent();
            this.Width = 280;
            this.Height = 380;
            this.Text = "Calculator v1.1";
            InitControls();
        }
     
        private void InitControls()
        {
            
            textBox1.Parent = this;
            textBox1.Location = new Point(12, 12);
            textBox1.Width = 240;
            textBox1.Height = 60;
            textBox1.Font = new Font("verdana", 14);
            textBox1.Name = "ResultBox";

            panelNumber.Parent = this;
            panelNumber.Location = new Point(12, 65);
            panelNumber.Width = 175;
            panelNumber.Height = 260;


            panelActoin.Parent = this;
            panelActoin.Location = new Point(198, 65);
            panelActoin.Width = 50;
            panelActoin.Height = 260;

            for (int y = 0; y < 4; y++)
                for (int x = 0; x < 3; x++)
                {
                    string caption = "7894561230C=";
                    Button Btn = new Button();
                    Btn.Name = Convert.ToString(caption[y * 3 + x]);
                    Btn.Parent = panelNumber;
                    Btn.Text = Convert.ToString(caption[y * 3 + x]);
                    Btn.Font = new Font("verdana", 14);
                    Btn.Width = 50;
                    Btn.Height = 50;
                    Btn.Left = x * 50;
                    Btn.Top = 50 * y;
                    Btn.Click += new System.EventHandler(buttonNumber_Click);
                }

            for (int x = 0; x < 4; x++)
            {
                string caption = "+-*/";
                Button Btn = new Button();
                Btn.Parent = panelActoin;
                Btn.Text = Convert.ToString(caption[x]);
                Btn.Name = Convert.ToString(caption[x]);
                Btn.Font = new Font("verdana", 14);
                Btn.Width = 50;
                Btn.Height = 50;
                Btn.Left = 0;
                Btn.Top = 50 * x;
                Btn.Click += new System.EventHandler(buttonAction_Click);
            }
        }

        private void buttonNumber_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (btn.Text == "=")
                Calculate();
            else if (btn.Text == "C")
                textBox1.Text = "";
            else
                textBox1.Text += btn.Text;
        }

        private void Calculate()
        {
            try
            {
                string bStr = textBox1.Text;
                b = Convert.ToInt32(bStr);
                textBox1.Text = Calculator.Calculate(a, b, operation);
            }
            catch (DivideByZeroException) { MessageBox.Show("divide by zero is not supported", "DivideByZeroException"); }
            catch (FormatException) { MessageBox.Show("Check arguments, and try again"); }
            catch (OverflowException) { MessageBox.Show("Too big number"); }
        }

        private void buttonAction_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            switch (btn.Text)
            {
                case "+":
                    {
                        setVal('+');
                        break;
                    }

                case "-":
                    {
                        setVal('-');
                        break;
                    }

                case "*":
                    {
                        setVal('*');
                        break;
                    }

                case "/":
                    {
                        setVal('/');
                        break;
                    }

                default: break;
            }
        }

        private void setVal(char c)
        {
            try
            {
                string aStr = textBox1.Text;
                a = Convert.ToInt32(aStr);
                operation = c;
                textBox1.Clear();
            }
            catch (FormatException) { MessageBox.Show("Check arguments, and try again"); }
            catch (OverflowException) { MessageBox.Show("Too big number"); }
        }

    }
}
